package com.ruirenyun.util.annotation.validator;

import org.springframework.validation.annotation.Validated;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @author Deng Ye
 * @version 1.0
 * @date 2020/9/21
 * @description
 * @modifiedBy
 */
@Validated
public class FutureValidator implements ConstraintValidator<Future, Date> {
    @Override
    public boolean isValid(Date value, ConstraintValidatorContext context) {
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        int day = instance.get(Calendar.DAY_OF_MONTH);
        instance.setTime(value);
        int nowYear = instance.get(Calendar.YEAR);
        int nowMonth = instance.get(Calendar.MONTH);
        int nowDay = instance.get(Calendar.DAY_OF_MONTH);
        if (nowYear < year) {
            return false;
        } else if (nowYear > year) {
            return true;
        } else {
            if (nowMonth > month) {
                return true;
            } else if (nowMonth < month) {
                return false;
            } else {
                return nowDay >= day;
            }
        }
    }
}
